package schema

import (
	"github.com/shopspring/decimal"
	"lianzi_gos/internal/config"
	"lianzi_gos/pkg/util"
	"time"
)

// 产品属性表
type GoodsAttr struct {
	ID         string          `json:"id" gorm:"size:20;primarykey;"` // Unique ID
	Unit       decimal.Decimal `json:"unit" gorm:"type:decimal(10,2);not null;comment:'价格';default:0"`
	Price      decimal.Decimal `json:"price" gorm:"type:decimal(10,2);not null;comment:'估价格';default:0"`
	Name       string          `json:"name" gorm:"size:250;index;default:'';comment:'名称';"`      // 名称
	Code       string          `json:"code" gorm:"size:20;index;default:'';comment:'设备编号';"`     // 设备编号
	AsName     string          `json:"as_name" gorm:"size:190;index;default:'';comment:'商品别名';"` //商品别名
	Spec       string          `json:"spec" gorm:"size:190;default:'';comment:'商品别名';"`          //商品别名
	VideoUrl   string          `json:"video_url" gorm:"size:190;default:'';comment:'视频链接';"`     //视频链接
	Image      string          `json:"image" gorm:"type:text;index;comment:'图片';"`
	PriceImage string          `json:"price_image" gorm:"type:text;comment:'原购买凭证';"` //原购买凭证
	Year       int64           `json:"year" gorm:"index;default:0;comment:'年份';"`
	Bid        string          `json:"bid" gorm:"size:20;index;default:'';comment:'品牌';"`      // 品牌 ID
	Size       string          `json:"size" gorm:"size:20;index;default:'';comment:'尺寸';"`     // 尺寸
	Power      string          `json:"power" gorm:"size:20;index;default:'';comment:'功率';"`    // 功率
	Diameter   string          `json:"diameter" gorm:"size:20;index;default:'';comment:'直径';"` // 直径
	Capacity   string          `json:"capacity" gorm:"size:20;index;default:'';comment:'容量';"` // 容量
	Method     int64           `json:"method" gorm:"index;default:0;comment:'使用方式';"`          // 使用方式
	Cid        string          `json:"cid" gorm:"size:20;index;default:'';comment:'分类ID';"`    // 分类ID
	Stock      int64           `json:"stock" gorm:"size:20;default:0;comment:'库存';"`
	Fineness   int64           `json:"fineness" gorm:"default:0;comment:'成色';"`
	BName      string          `json:"b_name" gorm:"size:50;index;default:'';comment:'品牌名'"`
	Desc       string          `json:"desc" gorm:"size:200;default:'';comment:'备注'"`
	Gid        string          `json:"gid" gorm:"index;size:20;default:'';comment:'商品ID';"` // Sequence for sorting
	Status     string          `json:"status" gorm:"size:20;index"`                         // Status of dictionary (disabled, enabled)
	CreatedAt  time.Time       `json:"created_at" gorm:"index;"`                            // Create time
	UpdatedAt  time.Time       `json:"updated_at" gorm:"index;"`                            // Update time
}

func (a *GoodsAttr) TableName() string {
	return config.C.FormatTableName("goods_attr")
}

type GoodsAttrStatus struct {
	Status string `json:"status" binding:"required"` //驳回
}
type GoodsAttrPrice struct {
	Price decimal.Decimal `json:"price" binding:"required"` //驳回
}

// Defining the query parameters for the `GoodsAttr` struct.
type GoodsAttrQueryParam struct {
	util.PaginationParam
	//Gid    string `form:"gid"` // Status of dictionary (disabled, enabled)
	Code   string `form:"code"`    // Status of dictionary (disabled, enabled)
	Status string `form:"status"`  // Status of dictionary (disabled, enabled)
	AttrId string `form:"attr_id"` //
	Name   string `form:"name"`    // 搜索名字
	Source int    `form:"source"`  // 搜索名字
	Types  string `form:"types"`   // tow 是根据全店搜索
}

// Defining the query options for the `GoodsAttr` struct.
type GoodsAttrQueryOptions struct {
	Name   string `form:"name"`
	Status string `form:"status"`
	util.QueryOptions
}

// Defining the query result for the `GoodsAttr` struct.
type GoodsAttrQueryResult struct {
	Data       GoodsAttrs
	PageResult *util.PaginationResult
}

// Defining the query result for the `GoodsAttr` struct.
type GoodsAttrResult struct {
	Data GoodsAttrs
}

// Defining the slice of `GoodsAttr` struct.
type GoodsAttrs []*GoodsAttr

func (a GoodsAttrs) Len() int {
	return len(a)
}

func (a GoodsAttrs) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

// Defining the data structure for creating a `GoodsAttr` struct.
type GoodsAttrForm struct {
	Name       string          `json:"name" binding:"required,max=32"` // name
	Image      string          `json:"image" binding:"required"`       //image
	Stock      int64           `json:"stock" binding:"required"`       //image
	Cid        string          `json:"cid" binding:"required,max=20"`  // 业态
	Code       string          `json:"code" binding:"required,max=20"` // 设备编号  与 goods 表 同级
	Unit       decimal.Decimal `json:"unit"`                           //  价格
	Year       int64           `json:"year" binding:"required"`        //年费
	BName      string          `json:"b_name" binding:"required"`      // 品牌
	Size       string          `json:"size" binding:"required"`        // 尺寸
	Capacity   string          `json:"capacity" binding:"required"`    //容量
	Diameter   string          `json:"diameter" binding:"required"`    // 直径
	Power      string          `json:"power" binding:"required"`       // 功率
	AsName     string          `json:"as_name"`                        // 别名
	VideoUrl   string          `json:"video_url" binding:"required"`   // 视频链接
	PriceImage string          `json:"price_image" binding:"required"` // 原价格照片
	Method     int64           `json:"method" binding:"required"`      // 使用方式
	Desc       string          `json:"Desc"`                           // 其他描述
}

// A validation function for the `GoodsAttrForm` struct.
func (a *GoodsAttrForm) Validate() error {
	return nil
}
func (a *GoodsAttrStatus) FillToStatus(goodsAttr *GoodsAttr) error {
	goodsAttr.Status = a.Status
	return nil
}
func (a *GoodsAttrPrice) FillToPrice(goodsAttr *GoodsAttr) error {
	goodsAttr.Price = a.Price
	return nil
}

// Convert `GoodsAttrForm` to `GoodsAttr` object.
func (a *GoodsAttrForm) FillTo(goodsAttr *GoodsAttr) error {
	goodsAttr.Name = a.Name
	goodsAttr.Image = a.Image
	goodsAttr.PriceImage = a.PriceImage // 购买凭证图片
	goodsAttr.BName = a.BName
	goodsAttr.Cid = a.Cid
	goodsAttr.Size = a.Size
	goodsAttr.Diameter = a.Diameter
	goodsAttr.Power = a.Power
	goodsAttr.Method = a.Method
	goodsAttr.Capacity = a.Capacity
	goodsAttr.Code = a.Code
	goodsAttr.VideoUrl = a.VideoUrl
	goodsAttr.Year = a.Year
	goodsAttr.Desc = a.Desc
	goodsAttr.Unit = a.Unit
	goodsAttr.Stock = a.Stock
	goodsAttr.Status = "disabled"
	return nil
}
